<?php
class Useradmin_Data_Editform {

	private $form;
	private $database;
	private $secure;

	public function __construct(){
		$this->form = new Core_Driver_Firstform;
		$this->database = new Core_Driver_MysqlImproved;
		$this->secure = new Core_Data_Secure;
	}

	public function getForm(array $getVars){
		$userData = $this->getUserValues($getVars);
		$addForm = $this->form->startForm(
			array(
			"id" => "addForm",
					"method" => "post",
					"action" => SERVER_ROOT . "?useradmin&page=updated"
		)
		);
		$addForm .= $this->form->inputForm(
			array(
			"type" => "hidden",
					"name" => "oldUserName",
					"value" => $userData['username']
		),
				null
		);
		$addForm .= ADD_USER_MESSAGE;
		$addForm .= "<ul>";
		$addForm .= "<li>";
		$addForm .= $this->form->labelForm(
			array(
			"for" => "username"
		),
				EDIT_USER_USERNAME,
				null
		);
		$addForm .= $this->form->inputForm(
			array(
			"id" => "username",
					"name" => "username",
					"type" => "text",
					"maxlegth" => "255",
					"value" => $userData['users_username']
		),
			"required autofocus"
		);
		$addForm .= "</li>";
		$addForm .= "<li>";
		$addForm .= $this->form->labelForm(
				array(
						"for" => "realname"
				),
				EDIT_USER_REALNAME,
				null
		);
		$addForm .= $this->form->inputForm(
				array(
						"id" => "realname",
						"name" => "realname",
						"type" => "text",
						"maxlegth" => "255",
						"value" => $userData['users_realname']
				),
				"required autofocus"
		);
		$addForm .= "</li>";
		$addForm .= "<li>";
		$addForm .= $this->form->labelForm(
			array(
			"for" => "email"
		),
				EDIT_USER_EMAIL,
				null
		);
		$addForm .= $this->form->inputForm(
				array(
						"id" => "email",
						"name" => "email",
						"type" => "email",
						"maxlegth" => "255",
						"value" => $userData['users_email']
				),
				"required"
		);
		$addForm .= "</li>";
		$addForm .= "<li>";
		$addForm .= $this->form->labelForm(
				array(
						"for" => "password"
				),
				EDIT_USER_PASSWORD,
				null
		);
		$addForm .= $this->form->inputForm(
				array(
						"id" => "password",
						"name" => "password",
						"type" => "password",
						"value" => $userData['users_password']
				),
				"required"
		);
		$addForm .= "</li>";
		$addForm .= "<li>";
		$addForm .= $this->form->labelForm(
				array(
						"for" => "banned"
				),
				EDIT_USER_ACTIVE,
				null
		);
		$addForm .= $this->form->selectForm(
				array(
						"id" => "banned",
						"name" => "banned"
				),
				"required",
				array(	
						$userData['users_banned'] => "Current value",
						"0" => "(0) Active",
						"1" => "(1) No active"
				)
		);
		$addForm .= "</li>";
		$addForm .= "<li>";
		$accessLevels = array();
		$accessLevels[$userData['users_access_level']] = "Current access level";
		$accessLevels = $this->getAccessLevels();
		$addForm .= $this->form->labelForm(
				array(
						"for" => "accessLevel"
				),
				EDIT_USER_ACCESS_LEVEL,
				null
		);
		$addForm .= $this->form->selectForm(
				array(
						"id" => "accessLevel",
						"name" => "accessLevel"
				),
				"required",
				$accessLevels
		);
		$addForm .= "</li>";
		$addForm .= "</ul>";
		$addForm .= '<div class = "buttons">';
		$addForm .= $this->form->inputForm(
				array(
						"id" => "submit",
						"name" => "submit",
						"type" => "submit",
						"value" => EDIT_USER_BUTTON
				),
				null
		);
		$addForm .= $this->form->endForm();
		$addForm .= "</div>";
		return $addForm;
	}
	
	private function getUserValues(array $getVars){
		$getVars = $this->secure->parseData($getVars);
		$this->database->connect();
		$email = $getVars['username'];
		$email = $this->database->escape($email);
		$this->database->prepare(
			"select * from users where users_email = '$email';"
		);
		$this->database->query();
		$userData = $this->database->fetch('array');
		return $userData;
	}
	
	private function getAccessLevels(){
		$this->database->connect();
		$this->database->prepare(
				"select * from privileges order by privileges_types"
		);
		$this->database->query();
		$accessLevels = array();
		while($accessData = $this->database->fetch('array')){
			$accessLevels[$accessData['privileges_types']] = "(".$accessData['privileges_types'].") ".$accessData['privileges_name'];
		}
		return $accessLevels;
	}
}